const  {users} = require('../models/init-models')();

const account = {
  login:(req, res, next) => {     
      const { username, password,verification } = req.body       
      if(req.session.captcha != verification) res.send("验证码不正确")

      users.findOne({where:{ username, password },raw:true}).then((result)=>{
          if(result){
            console.log(result)
            
            req.session.user=result.username
            req.session.thumb=result.thumb
            res.redirect('/users/')
          }
          else res.redirect('login')
        }).catch((error)=>{
          console.log(error)
        })    
  },
  regist:(req, res, next) => {
      //const { username, password } = req.body
      console.log(req.body)
      users.create(req.body).then((result)=>{
          console.log('注册成功')
              res.redirect('login')
      }).catch((error)=>{
          console.log('注册失败')
              res.redirect('regist') 
      }) 
  },
}



module.exports = account;
